package ch.fhnw.qualiscan.application.components;

import ch.fhnw.qualiscan.ApplicationData;
import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.core.LayoutComponent;
import ch.fhnw.qualiscan.core.db.HibernateSession;
import ch.fhnw.qualiscan.core.helpers.UserSessionHelper;
import ch.fhnw.qualiscan.core.helpers.UserSession;
import ch.fhnw.qualiscan.core.libraries.LoaderLibrary;

import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;

import eu.livotov.tpt.TPTApplication;
import eu.livotov.tpt.gui.dialogs.OptionDialog;
import eu.livotov.tpt.gui.dialogs.OptionDialog.OptionDialogResultListener;
import eu.livotov.tpt.gui.dialogs.OptionKind;
import eu.livotov.tpt.i18n.TM;

/**
 * This box is shown in the upper right corner and provides the user with information about profile information.
 * @author Benjamin Groehbiel
 *
 */
@SuppressWarnings("serial")
public class SecondNavigation extends LayoutComponent {

	UserSessionHelper userSession;
	HibernateSession hibernateSession;
	LoaderLibrary loaderLib;
	
	@Override
	public void buildComponent() {
		ApplicationData appData  = (ApplicationData) TPTApplication.getCurrentApplication().getMainWindow().getData();
		loaderLib = appData.getLoaderLib();
		setMargin(false, false, false, true);
		setHeight("100%");
		setSpacing(true);
		
		userSession = new UserSessionHelper();
		hibernateSession = new HibernateSession();
		UserSession user = userSession.getUserSession();

		if(userSession.isUserSessionSet()) {
			
			HorizontalLayout secondNaviWrap = new HorizontalLayout();
			secondNaviWrap.setHeight("100%");
			secondNaviWrap.setWidth("100%");
			
			// name
			HorizontalLayout nameWrap = new HorizontalLayout();
			nameWrap.setHeight("100%");
			nameWrap.setWidth("");
			
			Label name = new Label(user.getFirstname() + " " + user.getLastname());
			nameWrap.addComponent(name);
			nameWrap.setComponentAlignment(name, Alignment.MIDDLE_LEFT);
			
			// logout Button
			HorizontalLayout logoutButtonWrap = new HorizontalLayout();
			logoutButtonWrap.setHeight("100%");
			logoutButtonWrap.setWidth("100px");
			logoutButtonWrap.setStyleName("secondNavItem");
			
			Button logoutButton = new Button(TM.get("action.logout"));
			logoutButton.setStyleName(Button.STYLE_LINK);
			logoutButton.addListener(new Button.ClickListener() {
				public void buttonClick(ClickEvent event) {
					final OptionDialog dlg = new OptionDialog (TPTApplication.getCurrentApplication());
                    dlg.showConfirmationDialog(TM.get("action.logout"), TM.get("loginfo.confirm"), new OptionDialogResultListener() {
                            public void dialogClosed(OptionKind arg0) {
                                    if(arg0 == OptionKind.OK) {
                                    		hibernateSession.closeSession();
                                            userSession.deleteUserSession();
                                            componentLib.userLoggedOut();
                                            loaderLib.setUrlFragment(0, "Start");
                                    }
                            }
                    });
				}
			});
			logoutButton.setHeight("");
			logoutButton.setClickShortcut(KeyCode.ESCAPE, null);
			logoutButton.setIcon(Config.ICON_LOGOFF);
			
			logoutButtonWrap.addComponent(logoutButton);
			logoutButtonWrap.setComponentAlignment(logoutButton, Alignment.MIDDLE_CENTER);
			
			secondNaviWrap.addComponent(nameWrap);
			secondNaviWrap.addComponent(logoutButtonWrap);
			secondNaviWrap.setExpandRatio(nameWrap, 1.0f);
	
			addComponent(secondNaviWrap);
			setComponentAlignment(secondNaviWrap, Alignment.MIDDLE_LEFT);
			
		} else {
			Label loggedOut = textHelper.span(TM.get("user.loggedout"));
			addComponent(loggedOut);
			setComponentAlignment(loggedOut, Alignment.MIDDLE_LEFT);
		}
		
	}
	
}
